
import Toast from '@vant/weapp/toast/toast';
const app = getApp()
const api = app.globalData.api

Page({

  /**
   * 页面的初始数据
   */
  data: {
    info:{},
    id:'',
    ico:'',
    dormitory:'',
    dormitoryId:'',
    time_popup_show:false,
    type_popup_show:false,
    typeOption:[],
    recordDate:'',
    currentTimestamp:'',
    mealType_selected:'',
    nowType:'',
    num:1,
    isHealth:'合格',
    isPower:'合格',
    isViolation:'合格',
  },


  onRadioChange(event){
    let type = event.currentTarget.dataset.type;
    if( type == 'health'){
      this.setData({
        isHealth: event.detail,
      });
    }else if(type == 'power'){
      this.setData({
        isPower: event.detail,
      });
    }else if(type == 'violation'){
      console.log(event.detail)
      this.setData({
        isViolation: event.detail,
      });
    }
   
  },
  /**
   * 选择宿舍
   */
  chooseDormitory(e){
    
    let that = this;
    api.curlPost('?cla=dormitoryApp&fun=dormitory').then((res)=>{
        that.setData({
          type_popup_show:true,
          typeOption:res.data
        })
      })
  },

  /**餐别确认 */
  onTypeConfirm(event){
    const { picker, value, index } = event.detail;
    this.setData({
      dormitory:value.name,
      dormitoryId:value.id,
      type_popup_show:false
    })
  },
  onTypeCancel(e){
    this.setData({
      type_popup_show:false
    })
  },

  commit(e){
    let para = e.detail.value;
    para.id = this.data.id;
    para.dormitoryId = this.data.dormitoryId;
    para.isViolation = this.data.isViolation;
    para.isHealth = this.data.isHealth;
    para.isPower = this.data.isPower;
    api.curlPost('?cla=dormitoryApp&fun=liveEdit',para).then((res)=>{
      Toast(res.warn);
       wx.navigateBack({
         delta: 1,
       })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let id = api.isBlank(options.id)?'':options.id;
    let that = this;
    if(id){
      api.curlPost('?cla=dormitoryApp&fun=detail',{id:id}).then((res)=>{
        that.setData({
          dormitory:res.data.result.dormitoryName,
          dormitoryId:res.data.result.dormitoryId,
          recordDate:res.data.result.recordDate,
          isHealth:res.data.result.isHealth,
          isPower:res.data.result.isPower,
          isViolation:res.data.result.isViolation,
          id:res.data.result.id,
          info:res.data.result
        })
      })
    }
    
    let currentTimestamp = new Date().getTime();
    this.setData({
      recordDate: api.formatDateCRM('Y-m-d',currentTimestamp),
    })
  },
 
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
/**选择时间 */
chooseTime(e){
  let currentTimestamp = this.data.recordDate?new Date(api.rowTocol(this.data.recordDate)).getTime():new Date().getTime();
  this.setData({
    time_popup_show:true,
    currentTimestamp:currentTimestamp
  })

},
/**日期选择确认 */
timeConfirm(event){

  let date = new Date(event.detail);
    this.setData({
      recordDate: api.formatDateCRM('Y-m-d',date),
      time_popup_show:false,
    })
    this.getMenu();
},
timeCancel(e){
  this.setData({
    time_popup_show:false
  })
},
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})